object-sorter.ts ➔ collectObjectEntries   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 9
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
crap 1
1
import { SortOptions, ObjectType, SortedEntry } from './types';
2 1
import { compareObjectKeys } from './comparators';
3
4 1
export function sortObject(obj: ObjectType, options: SortOptions): ObjectType {
5 1398
  const entries = collectObjectEntries(obj);
6 1398
  const sortedEntries = sortObjectEntries(entries, options.ascending);
7 1398
  return createSortedObject(sortedEntries, options);
8
}
9
10
// Object entry handling
11
function collectObjectEntries(obj: ObjectType): SortedEntry[] {
12 1398
  const stringEntries = Object.entries(obj);
13 1398
  const symbolEntries = Object.getOwnPropertySymbols(obj).map(
14 5
    (symbol) => [symbol, obj[symbol]] as SortedEntry
15
  );
16
17 1398
  return [...stringEntries, ...symbolEntries];
18
}
19
20
function sortObjectEntries(
21
  entries: SortedEntry[],
22
  ascending: boolean
23
): SortedEntry[] {
24 1398
  return entries.sort(([keyA], [keyB]) =>
25 102
    compareObjectKeys(keyA, keyB, ascending)
26
  );
27
}
28
29
function createSortedObject(
30
  entries: SortedEntry[],
31
  options: SortOptions
32
): ObjectType {
33 1472
  const sortedEntries = entries.map(([key, value]) => [
34
    key,
35
    sortRecursively(value, options),
36
  ]);
37
38 59
  return Object.fromEntries(sortedEntries);
39
}
40
41
import { sortRecursively } from './core';
42